﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.ShopServiceReference;
using ServerApp;
using Server;

namespace Client.View.Components
{
    public partial class OrdersView : UserControl, IOrdersView
    {
        #region Constructor

        public OrdersView()
        {
            InitializeComponent();
            OrdersPresenter ordersPresenter = new OrdersPresenter(this);
            dataGridViewOrders.AutoGenerateColumns = true;

        }

        #endregion Constructor

        #region event

        public event EventHandler OnCreate;


        public event EventHandler OnGetFullOrder;


        public event ReserveOrder OnReserveOrder;

        #endregion

        #region publicMethods


        public void FillTheOrder(OrderFull order)
        {
            if (order != null)
            {
                if (order.Customer != null)
                {
                    this.textBoxCustomer.Text = order.Customer.FIO;
                    this.textBoxPhone.Text = order.Customer.Phone;
                }

                textBoxComment.Text = order.Comment;
                comboBoxStatus.Text = order.Status.ToString();
                dateTimePickerStart.Value = order.CreatedDateTime;

                if (order.Executor != null)
                    textBoxExecutor.Text = order.Executor.FIO;                

                if (order.ClosedDateTime != null && order.ClosedDateTime > dateTimePickerEnd.MinDate && order.ClosedDateTime < dateTimePickerEnd.MaxDate)
                    dateTimePickerEnd.Value = order.ClosedDateTime;

                if (order.Composition != null)
                {                
                    for (int i=0; i< order.Composition.Count(); i++)
                    {
                        dataGridViewProducts.Rows.Add();
                        dataGridViewProducts.Rows[i].Cells["Name"].Value = order.Composition[i].Product.Name;
                        dataGridViewProducts.Rows[i].Cells["Price"].Value = order.Composition[i].Cost;
                        dataGridViewProducts.Rows[i].Cells["Count"].Value = order.Composition[i].Count;
                    }
                }
            }

        }


        public void FillOrderTable(List<OrderView> list)
        {
            if (list != null)
            {
                m_orderList = list;
                bindingSourceOrders.DataSource = m_orderList;
                this.bindingNavigatorOrders.BindingSource = bindingSourceOrders;
                dataGridViewOrders.DataSource = bindingSourceOrders;
                dataGridViewOrders.Update();
                dataGridViewOrders.Columns["Id"].Visible = false;
                dataGridViewOrders.Columns["IsNull"].Visible = false;
            }
        }


        public void UpdateOrder(OrderFull order)
        {
            clearFields();
            FillTheOrder(order);
        }

        #endregion

        private List<OrderView> m_orderList = null;

        #region privateMethods

        private void ordersView_Load(object sender, EventArgs e)
        {
            if (this.OnCreate != null)
            {
                this.OnCreate(this, e);
            }
        }


        private void dataGridViewOrders_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            clearFields();
            OrdersEventArgs oea = new OrdersEventArgs();

            oea.Id = Convert.ToInt32(dataGridViewOrders.Rows[dataGridViewOrders.SelectedCells[0].RowIndex].Cells["Id"].Value);
            oea.Language = "ru";
            if (this.OnGetFullOrder != null)
            {
                this.OnGetFullOrder(this, oea);
            }
        }


        private void toolStripButtonReserveOrder_Click(object sender, EventArgs e)
        {
            if (dataGridViewOrders.SelectedCells[0] != null)
            {
                if (OnReserveOrder != null)
                {
                    ///EXECUTOR!!!!!
                    this.OnReserveOrder(Convert.ToInt32(dataGridViewOrders.Rows[dataGridViewOrders.SelectedCells[0].RowIndex].Cells["Id"].Value), 1);
                }
            }
        }

        private void clearFields()
        {
            this.textBoxCustomer.Text = string.Empty;
            this.textBoxPhone.Text = string.Empty;
            this.textBoxComment.Text = string.Empty;
            this.textBoxExecutor.Text = string.Empty;
            comboBoxStatus.Text = string.Empty;
            dateTimePickerStart.Value = DateTime.Parse("01.01.2000");
            dateTimePickerEnd.Value = DateTime.Parse("01.01.2000");
            dataGridViewProducts.Rows.Clear();
        }

        #endregion


        



    }//class
}
